package com.project.srtp.secure.util;

import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.kie.dmn.feel.lang.Type;
import org.kie.dmn.feel.lang.ast.BaseNode;
import org.kie.dmn.feel.lang.types.DefaultBuiltinFEELTypeRegistry;
import org.kie.dmn.feel.lang.types.FEELTypeRegistry;
import org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor;
import org.kie.dmn.feel.parser.feel11.FEEL_1_1Lexer;
import org.kie.dmn.feel.parser.feel11.FEEL_1_1Parser;
import org.kie.dmn.feel.parser.feel11.ParserHelper;

import java.util.Collections;
import java.util.Map;

/**
 * @author 曹行阳
 * @version 0.1
 * @date 2025/6/11 16:00
 */
public class FeelAstUtil {

    public static BaseNode parseExpression(String feelExpression) {
        CharStream input = CharStreams.fromString(feelExpression);
        FEEL_1_1Lexer lexer = new FEEL_1_1Lexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        FEEL_1_1Parser parser = new FEEL_1_1Parser(tokens);

        parser.setHelper(new ParserHelper(null));

        parser.removeErrorListeners();
        parser.setErrorHandler(new BailErrorStrategy());

        FEELTypeRegistry typeRegistry = DefaultBuiltinFEELTypeRegistry.INSTANCE;
        ASTBuilderVisitor visitor = new ASTBuilderVisitor(Map.of(), typeRegistry);

        return visitor.visit(parser.expression());
    }
}

